package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.MyJDBC;

public class ParameterAdd extends BaseServlet {
	private static final long serialVersionUID = 1L;


	public boolean process(HttpServletRequest request, HttpServletResponse response,HttpSession session, PrintWriter out, MyJDBC myJDBC,  String menu)throws Exception {
		int i_flag = 0;

		// ===============
		String s_parenmeterid = request.getParameter("parenmeterid");
		String returnvalue = s_parenmeterid;
		String listnum = request.getParameter("listnum");

		String s_sql = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String s_addfs = request.getParameter("addfs"); // 添加方式
		String s_cx = "";
		String s_zx = "";
		if ("1".equals(s_addfs)) {// 添加子项
			s_cx = "";
			s_zx = request.getParameter("zx1");
		} else {// 添加参数和子项
			s_cx = request.getParameter("cs2");
			s_zx = request.getParameter("zx2");
		}

		if ("2".equals(s_addfs)) { // 添加参数和子项
			s_sql = "select max(paraid) from parameter";
			ps = myJDBC.prepareStatement(s_sql);
			rs = ps.executeQuery();
			rs.next();
			int s_count = rs.getInt(1);
			s_sql = null;
			rs.close();
			rs = null;
			ps.close();
			ps = null;

			s_sql = "insert into parameter (paraid,parentid,paraname,status) values(?,?,?,?)";
			ps = myJDBC.prepareStatement(s_sql);

			ps.setString(1, Integer.toString(s_count + 1));
			ps.setString(2, "0");
			ps.setString(3, s_cx);
			ps.setString(4, "0");
			i_flag = ps.executeUpdate();
			// s_sql = null;
			// ps.close();
			// ps = null;

			// s_sql = "insert into parameter (paraid,parentid,paraname,status) values(?,?,?,?)";
			// ps = myJDBC.prepareStatement(s_sql);
			if (i_flag == 1) {
				ps.setString(1, Integer.toString(s_count + 2));
				ps.setString(2, Integer.toString(s_count + 1));
				ps.setString(3, s_zx);
				ps.setString(4, "0");
				i_flag = ps.executeUpdate();
			}

			s_sql = null;
			ps.close();
			ps = null;
			returnvalue = Integer.toString(s_count + 1);
		} else {
			s_sql = "insert into parameter (paraid,parentid,paraname,status,listnum) "
					+ "values((select max(paraid)+1 from parameter),?,?,?,?)";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, s_parenmeterid);
			ps.setString(2, s_zx);
			ps.setString(3, "0");
			ps.setString(4, listnum);
			i_flag = ps.executeUpdate();
			s_sql = null;
			ps.close();
			ps = null;
		}

		String act = "参数添加";
		if (i_flag > 0) {
			alertAndReturnValue(out,act + "成功", returnvalue);
		} else {
			alert(out,act+ "失败");
		}
		return i_flag > 0;
	}
}
